package courses;

import javax.swing.*;
import java.awt.*;

public class ChangeColor {
    JFrame jf;
    JLabel label;
    Container con;

    public ChangeColor() {
        jf = new JFrame("变色窗口");
        label = new JLabel("我会改变颜色");
        con = jf.getContentPane();
    }

    class MyThread extends Thread {
        Color color[] = {Color.CYAN, Color.red, Color.ORANGE, Color.black, Color.blue};
        int i = 0;
        boolean temp = false;
        @Override
        public void run() {
            System.out.println(color.length);
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                con.setBackground(color[i]);
                if (temp== false) {
                    i++;
                    if (i >=color.length) {
                        System.out.println("true");
                        temp= true;
                        i--;
                    } else {
                        System.out.println("false");
                        temp = false;
                    }
                } else {
                    System.out.println("else");
                    i--;
                    if (i ==0) {
                        System.out.println("true");
                        temp= true;
                        i++;
                    }
                }
            }
        }
    }


    public void init() {
        jf.add(label);
        jf.setVisible(true);
        jf.setSize(600, 600);

        new MyThread().start();
    }

    public static void main(String[] args) {
        new ChangeColor().init();
    }
}
